package com.hex.liyue.commonmodule.utils;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.listener.UploadFileListener;

import java.io.File;

/**
 * Created by yukuo on 2016/6/16.
 * 这是一个阿里云上传或者下载文件的帮助类
 */
public class OssUtils {
    private final OSSClient oss;
    private String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
    private String testBucket = "liyue";
    private String resultPath = "http://" + testBucket + ".oss-cn-qingdao.aliyuncs.com";
    private String accessKeyId = "mILdVDmK9mvhwXqQ";
    private String accessKeySecret = "4LsAB5jjmRgC0ei5HzyiC7DlRa6FOZ";

    public String objectKeyVideo = "oss/android/video/";
    public String objectKeyUserBankInfo= "oss/android/user_bank_info/";
    public String objectKeyUserAvater= "oss/android/user_avater/";
    public String objectKeyUserPhoto= "oss/android/user_photo/";


    static OssUtils ossUtils;

    private OssUtils() {
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSSLog.enableLog();
        oss = new OSSClient(LiYueApplication.getInstance(), endpoint, credentialProvider, conf);
    }

    public static OssUtils getInstance() {
        if (ossUtils == null) {
            synchronized (OssUtils.class) {
                if (ossUtils == null) {
                    ossUtils = new OssUtils();
                }
            }
        }
        return ossUtils;
    }

    /**
     * 从本地文件上传，使用非阻塞的异步接口
     *
     * @param key            文件名字
     * @param uploadFilePath 文件路径
     */
    public void uploadfile(final String key, String uploadFilePath, final UploadFileListener uploadFileListener) {
        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(testBucket, key, uploadFilePath);

        // 异步上传时可以设置进度回调
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                if (uploadFileListener != null) {
                    uploadFileListener.onProgress(currentSize / totalSize);
                }
            }
        });

        OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {

                if (uploadFileListener != null) {
                    uploadFileListener.onSucess(resultPath + File.separator + key);
                }
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                if (uploadFileListener != null) {
                    uploadFileListener.onFailure();
                }
            }
        });
    }

}
